package com.zsc.controller.admin;

import com.zsc.dto.UpdateUserDTO;
import com.zsc.dto.UserPageQueryDTO;
import com.zsc.entity.User;
import com.zsc.result.PageResult;
import com.zsc.result.Result;
import com.zsc.service.UserService;
import com.zsc.utils.CopyUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author Enkidu
 * @Date 2024/6/13 16:48
 */
@RestController("adminUserController")
@Slf4j
@RequestMapping("/admin/user")
@Api(tags = "管理端 用户管理")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户分页查询
     *
     * @param userPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("用户分页查询")
    public Result<PageResult> page(UserPageQueryDTO userPageQueryDTO) {
        log.info("用户分页查询，查询参数为：{}", userPageQueryDTO);
        PageResult pageResult = userService.pageQuery(userPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 编辑用户信息
     *
     * @param updateUserDTO
     * @return
     */
    @PutMapping
    @ApiOperation("编辑用户信息")
    public Result update(@RequestBody UpdateUserDTO updateUserDTO) {
        log.info("编辑用户信息：{}", updateUserDTO);
        User user = CopyUtil.copy(updateUserDTO, User.class);
        userService.updateById(user);
        return Result.success();
    }

    /**
     * 根据用户id查询用户信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询用户信息")
    public Result<User> getById(@PathVariable Long id) {
        log.info("查询用户信息，用户id:{}", id);
        User user = userService.getById(id);
        return Result.success(user);
    }

    @DeleteMapping
    @ApiOperation("删除用户")
    public Result delete(Long id) {
        log.info("删除用户，用户id:{}", id);
        userService.removeById(id);
        return Result.success();
    }
}
